package com.ahaoyou.datasource;

import com.ahaoyou.annotation.DataSource;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/**
 * @Description
 * @Author LH
 * @Date 2021-09-26 15:51
 * dataSource的Aop动态数据源
 */
@Component
@Aspect
@Slf4j
public class DynamicDataSourceAspect {
    /**
     * 拦截我们的注解
     *
     * @param point
     * @param dataSource
     * @throws Throwable
     */
    @Before("@annotation(dataSource)")
    public void changeDataSource(JoinPoint point, DataSource dataSource) throws Throwable {
        //获取注解中内容
        String value = dataSource.value();
        if (value != null && value.length() > 0) {
            DynamicDataSourceContextHolder.setDataSource(value);
            MethodSignature methodSignature = (MethodSignature) point.getSignature();
            String methodName = methodSignature.getMethod().getName();
            log.info("方法名称:{},数据源名称:{}", methodName, value);
        }

    }
}
